package com.dianping.cat.consumer.matrix.model.transform;

import com.dianping.cat.consumer.matrix.model.entity.Matrix;
import com.dianping.cat.consumer.matrix.model.entity.MatrixReport;
import com.dianping.cat.consumer.matrix.model.entity.Ratio;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.xml.sax.Attributes;











public class DefaultSaxMaker
  extends Object
  implements IMaker<Attributes>
{
  public String buildDomain(Attributes attributes) { throw new UnsupportedOperationException(); }


  
  public Matrix buildMatrix(Attributes attributes) {
/*  30 */     String type = attributes.getValue("type");
    String name = attributes.getValue("name");
    String count = attributes.getValue("count");
/*  33 */     String totalTime = attributes.getValue("totalTime");
/*  34 */     String url = attributes.getValue("url");
/*  35 */     Matrix matrix = new Matrix(name);

/*  37 */     if (type != null) {
/*  38 */       matrix.setType(type);
    }
    
/*  41 */     if (count != null) {
/*  42 */       matrix.setCount(((Integer)convert(Integer.class, count, Integer.valueOf(0))).intValue());
    }
    
/*  45 */     if (totalTime != null) {
/*  46 */       matrix.setTotalTime(((Long)convert(Long.class, totalTime, Long.valueOf(0L))).longValue());
    }
    
/*  49 */     if (url != null) {
/*  50 */       matrix.setUrl(url);
    }
    
/*  53 */     return matrix;
  }

  
  public MatrixReport buildMatrixReport(Attributes attributes) {
/*  58 */     String domain = attributes.getValue("domain");
/*  59 */     String startTime = attributes.getValue("startTime");
/*  60 */     String endTime = attributes.getValue("endTime");
/*  61 */     MatrixReport matrixReport = new MatrixReport(domain);

/*  63 */     if (startTime != null) {
/*  64 */       matrixReport.setStartTime(toDate(startTime, "yyyy-MM-dd HH:mm:ss", null));
    }
    
/*  67 */     if (endTime != null) {
/*  68 */       matrixReport.setEndTime(toDate(endTime, "yyyy-MM-dd HH:mm:ss", null));
    }
    
/*  71 */     return matrixReport;
  }

  
  public Ratio buildRatio(Attributes attributes) {
/*  76 */     String type = attributes.getValue("type");
/*  77 */     String min = attributes.getValue("min");
    String max = attributes.getValue("max");
/*  79 */     String totalCount = attributes.getValue("totalCount");
/*  80 */     String totalTime = attributes.getValue("totalTime");
/*  81 */     String url = attributes.getValue("url");
/*  82 */     Ratio ratio = new Ratio(type);

/*  84 */     if (min != null) {
/*  85 */       ratio.setMin(((Integer)convert(Integer.class, min, Integer.valueOf(0))).intValue());
    }
    
/*  88 */     if (max != null) {
/*  89 */       ratio.setMax(((Integer)convert(Integer.class, max, Integer.valueOf(0))).intValue());
    }
    
/*  92 */     if (totalCount != null) {
/*  93 */       ratio.setTotalCount(((Integer)convert(Integer.class, totalCount, Integer.valueOf(0))).intValue());
    }
    
/*  96 */     if (totalTime != null) {
/*  97 */       ratio.setTotalTime(((Long)convert(Long.class, totalTime, Long.valueOf(0L))).longValue());
    }
    
/* 100 */     if (url != null) {
/* 101 */       ratio.setUrl(url);
    }
    
/* 104 */     return ratio;
  }

  
  protected <T> T convert(Class<T> type, String value, T defaultValue) {
/* 109 */     if (value == null || value.length() == 0) {
/* 110 */       return defaultValue;
    }
    
/* 113 */     if (type == Boolean.class || type == boolean.class)
/* 114 */       return (T)Boolean.valueOf(value); 
/* 115 */     if (type == Integer.class || type == int.class)
/* 116 */       return (T)Integer.valueOf(value); 
/* 117 */     if (type == Long.class || type == long.class)
/* 118 */       return (T)Long.valueOf(value); 
/* 119 */     if (type == Short.class || type == short.class)
/* 120 */       return (T)Short.valueOf(value); 
/* 121 */     if (type == Float.class || type == float.class)
/* 122 */       return (T)Float.valueOf(value); 
/* 123 */     if (type == Double.class || type == double.class)
/* 124 */       return (T)Double.valueOf(value); 
/* 125 */     if (type == Byte.class || type == byte.class)
/* 126 */       return (T)Byte.valueOf(value); 
/* 127 */     if (type == Character.class || type == char.class) {
/* 128 */       return (T)Character.valueOf(value.charAt(0));
    }
/* 130 */     return (T)value;
  }

  
  protected Date toDate(String str, String format, Date defaultValue) {
/* 135 */     if (str == null || str.length() == 0) {
/* 136 */       return defaultValue;
    }
    
    try {
/* 140 */       return (new SimpleDateFormat(format)).parse(str);
/* 141 */     } catch (ParseException e) {
/* 142 */       throw new RuntimeException(String.format("Unable to parse date(%s) in format(%s)!", new Object[] { str, format }), e);
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/matrix/model/transform/DefaultSaxMaker.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */